package com.fiberhome.ztelauncher.core.navigation;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.fiberhome.ztelauncher.core.FocusCarry;
import com.fiberhome.ztelauncher.uilib.NavItem;
import com.fiberhome.ztelauncher.util.BitmapUitl;
import com.fiberhome.ztelauncher.util.DisplayUtil;
import com.fiberhome.ztelauncher.util.TextStyleUtil;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataCommon;

import java.util.List;

/**
 * Created by xc on 2018/3/22.
 * 描述：首页导航栏adapter
 */

public class NavigationAdapter extends RecyclerView.Adapter<NavigationAdapter.MyViewHolder> implements FocusCarry {

    private final static int BOTTOM = 0;
    private final static int LEFT = 1;
    private final static int TOP = 2;
    private final static int RIGHT = 3;
    private Context context;
    private LaunchDataCommon.Navs navs;
    private List<LaunchDataCommon.Nav> listData;//数据
    private int direction;
    private int width;
    private int height;
    private int selectIndex;
    private OnSwitchPageListener mOnSwitchPageListener;
    private RecyclerView recyclerView;
    private MyViewHolder selectedHolder;

    public NavigationAdapter(Context context, RecyclerView recyclerView, LaunchDataCommon.Navs navs, int direction,
                             int width, int height) {
        this.context = context;
        this.navs = navs;
        this.direction = direction;
        this.width = width;
        this.height = height;
        listData = navs.navs;
        this.recyclerView = recyclerView;
    }

    @Override
    public NavigationAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        final View view = LayoutInflater.from(context).inflate(R.layout.item_navigation, parent, false);
        final NavItem item = new NavItem(context);
        setItemViewlp(item);
        item.setFocusable(true);
        item.setItemSelectedListener(new NavItem.OnItemSelectedListener() {
            @Override
            public void onSelected(NavItem navItem, boolean selected) {
                MyViewHolder holder = (MyViewHolder) recyclerView.getChildViewHolder(navItem);
                if (holder == null) return;
                if (selected) {
                    if (selectedHolder != null) {
                        selectedHolder.itemView.setSelected(false);
                    }
                    int position = holder.getAdapterPosition();
                    selectIndex = position;
                    selectedHolder = holder;
                    if (mOnSwitchPageListener != null) {
                        mOnSwitchPageListener.switchPage(selectIndex);
                    }
                    final LaunchDataCommon.Nav nav = listData.get(position);
                    BitmapUitl.loadBackground(context, holder.itemView, nav.backGroundImg);
                } else {
                    holder.itemView.setBackgroundColor(Color.parseColor("#00000000"));
                }
            }
        });
        item.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                MyViewHolder holder = (MyViewHolder) recyclerView.getChildViewHolder(view);
                if (holder == null) return;
                int position = holder.getAdapterPosition();
                final LaunchDataCommon.Nav nav = listData.get(position);
                if (b) {
                    BitmapUitl.loadBitmap(context, holder.ivIcon, nav.focusImg);
                    item.setSelected(true);
                } else {
                    BitmapUitl.loadBitmap(context, holder.ivIcon, nav.img);

                }
            }
        });
        return new NavigationAdapter.MyViewHolder(item);//将布局设置给holder
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final LaunchDataCommon.Nav nav = listData.get(position);
        holder.tvName.setText(nav.name);
        if (position == selectIndex) {
            BitmapUitl.loadBitmap(context, holder.ivIcon, nav.focusImg);
        } else {
            BitmapUitl.loadBitmap(context, holder.ivIcon, nav.img);
        }
    }

    private void setItemViewlp(View item){
        int itemWidth = 0;
        if (direction == TOP || direction == BOTTOM) {
            if (listData.size() > 6) {
                itemWidth = width / (listData.size() + 1);
            } else {
                itemWidth = (width / (listData.size() + 2));
            }
            if(itemWidth > 200){
                itemWidth = 200;
            }
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(DisplayUtil.px2dip(context, itemWidth),
                    ViewGroup.LayoutParams.MATCH_PARENT);
            item.setLayoutParams(lp);
        } else {
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    DisplayUtil.px2dip(context, 60));
            item.setLayoutParams(lp);
        }
    }


    @Override
    public boolean carry(int direction) {
        if (this.direction == LEFT || this.direction == RIGHT) {
            if (direction == KeyEvent.KEYCODE_DPAD_UP) {
                return selectIndex > 0;
            } else if (direction == KeyEvent.KEYCODE_DPAD_DOWN) {
                return selectIndex < getItemCount() - 1;
            }
        } else {
            if (direction == KeyEvent.KEYCODE_DPAD_LEFT) {
                return selectIndex > 0;
            } else if (direction == KeyEvent.KEYCODE_DPAD_RIGHT) {
                return selectIndex < getItemCount() - 1;
            }
        }
        return false;
    }

    @Override
    public boolean focus(boolean hasFocus) {
        if (hasFocus) {
            if (selectedHolder != null) {
                selectedHolder.itemView.requestFocus();
                return true;
            } else {
                return false;
            }

        }
        return true;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private ImageView ivIcon;

        public MyViewHolder(NavItem itemView) {
            super(itemView);
            tvName = itemView.getText();
            ivIcon = itemView.getIcon();

            tvName.setGravity(Gravity.CENTER);
            tvName.setTextColor(Color.parseColor(navs.fontColor));
            tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, navs.fontSize);
            tvName.getPaint().setTypeface(TextStyleUtil.getTextStyle(navs.fontStyle));
        }
    }

    /**
     * 切换page
     */
    public void setOnSwitchPageListener(OnSwitchPageListener onSwitchPageListener) {
        mOnSwitchPageListener = onSwitchPageListener;
    }

    /**
     * 切换page
     */
    public interface OnSwitchPageListener {
        void switchPage(int position);
    }
}
